共计 4219 个字符,预计需要花费 11 分钟才能阅读完成。
实现Cloneable接口并重写Object类中的clone()方法;
/**
* @author plm
* @create 2021/3/10 22:43
*/
class Son implements Cloneable {
private String name;
private int age;
public Son(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Son{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
class People implements Cloneable {
private String name;
private int age;
private Son son;
public People(String name, int age, Son son) {
this.name = name;
this.age = age;
this.son = son;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Son getSon() {
return son;
}
public void setSon(Son son) {
this.son = son;
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
@Override
public String toString() {
return "People{" +
"name='" + name + '\'' +
", age=" + age +
", son=" + son +
'}';
}
}
public class CloneDemo {
public static void main(String[] args) {
People people = new People("张三", 30, new Son("张小三", 2));
System.out.println("源对象:" + people);
try {
People newPeople = (People) people.clone();
System.out.println("克隆后新对象:" + newPeople);
System.out.println("======================================尝试修改克隆后新对象的属性====================================");
newPeople.getSon().setName("李小四");
newPeople.getSon().setAge(3);
System.out.println("源对象:" + people);
System.out.println("克隆后新对象:" + newPeople);
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
}
}
/* 输出
源对象:People{name='张三', age=30, son=Son{name='张小三', age=2}}
克隆后新对象:People{name='张三', age=30, son=Son{name='张小三', age=2}}
======================================尝试修改克隆后新对象的属性====================================
源对象:People{name='张三', age=30, son=Son{name='李小四', age=3}}
克隆后新对象:People{name='张三', age=30, son=Son{name='李小四', age=3}}
*/
修改克隆后新对象的属性,源对象的属性也变了——–浅拷贝
实现Serializable接口,通过对象的序列化和反序列化实现克隆;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
/**
* @author plm
* @create 2021/3/10 22:43
*/
class Son implements Serializable {
private static final long serialVersionUID = -5713945027627603702L;
private String name;
private int age;
public Son(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Son{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
class People implements Serializable {
private static final long serialVersionUID = -9102017020286042305L;
private String name;
private int age;
private Son son;
public People(String name, int age, Son son) {
this.name = name;
this.age = age;
this.son = son;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Son getSon() {
return son;
}
public void setSon(Son son) {
this.son = son;
}
@Override
public String toString() {
return "People{" +
"name='" + name + '\'' +
", age=" + age +
", son=" + son +
'}';
}
}
public class CloneDemo {
public static void main(String[] args) {
People people = new People("张三", 30, new Son("张小三", 2));
System.out.println("源对象:" + people);
try {
ByteArrayOutputStream bout = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bout);
oos.writeObject(people);
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bin);
People newPeople = (People) ois.readObject();
System.out.println("克隆后新对象:" + newPeople);
System.out.println("======================================尝试修改克隆后新对象的属性====================================");
newPeople.getSon().setName("李小四");
newPeople.getSon().setAge(3);
System.out.println("源对象:" + people);
System.out.println("克隆后新对象:" + newPeople);
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
}
/* 输出
源对象:People{name='张三', age=30, son=Son{name='张小三', age=2}}
克隆后新对象:People{name='张三', age=30, son=Son{name='张小三', age=2}}
======================================尝试修改克隆后新对象的属性====================================
源对象:People{name='张三', age=30, son=Son{name='张小三', age=2}}
克隆后新对象:People{name='张三', age=30, son=Son{name='李小四', age=3}}
*/
修改克隆后新对象的属性,源对象的属性不会变——–深度拷贝;
深度拷贝不仅仅拷贝该对象的成员属性,相关联的对象也会一起拷贝;
正文完